package com.dhsc.service.impl.ywjk.jzcl;

import com.dhsc.domain.jkzx.ywjk.jzcl.YcmxlsVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.domain.yypt.sys.TXtdmVo;
import com.dhsc.domain.yypt.yhzx.TJgxxVo;
import com.dhsc.domain.yypt.yhzx.TYhxxVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.common.ICommonDao;
import com.dhsc.local.dao.ywjk.jzcl.IYcmxlsDao;
import com.dhsc.local.dao.yypt.IYhzxDao;
import com.dhsc.service.ywjk.jzcl.IYcmxlsService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: YcmxlsServiceImpl
 * @Description: 历史异常明细
 * @date: 2023/8/29 14:54
 * @author: gx
 */
@Service
@Slf4j
public class YcmxlsServiceImpl implements IYcmxlsService {

    @Autowired
    private IYcmxlsDao ycmxlsDao;

    @Autowired
    private IYhzxDao yhzxDao;

    @Autowired
    private ICommonDao commonDao;

    @Override
    public LayuiTableVo queryYcmxls(YcmxlsVo ycmxlsVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(ycmxlsVo.getPage(), ycmxlsVo.getLimit());
            List<YcmxlsVo> list = ycmxlsDao.queryYcmxls(ycmxlsVo);
            PageInfo<YcmxlsVo> pageInfo = new PageInfo<>(list);

            if(CommonUtil.isNotEmptyList(list))
            {
                List<TYhxxVo> yhlist = yhzxDao.queryTYhxx(new TYhxxVo());
                List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
                TXtdmVo t = new TXtdmVo();
                t.setFldmstr("FQQD;YWQQZT");
                List<TXtdmVo> tXtdmVos = commonDao.queryZhTXtdmByFldmStr(t);

                for(int i = 0;i < list.size();i ++)
                {
                    for(int j = 0;j < yhlist.size();j ++)
                    {
                        if(list.get(i).getUserid() == yhlist.get(j).getUserid())
                        {
                            list.get(i).setCzgy_name(yhlist.get(j).getTruename());
                        }
                    }
                    for(int j = 0;j < jglist.size();j ++)
                    {
                        if(list.get(i).getYyb().equals(jglist.get(j).getJgdm()))
                        {
                            list.get(i).setYybmc(jglist.get(j).getBmmc());
                        }
                        if(list.get(i).getFsyyb().equals(jglist.get(j).getJgdm()))
                        {
                            list.get(i).setFsyybmc(jglist.get(j).getBmmc());
                        }
                    }
                    for(int j = 0;j < tXtdmVos.size();j ++)
                    {

                        if(tXtdmVos.get(j).getFldm().equals("FQQD"))
                        {
                            if (list.get(i).getFqqd().equals(tXtdmVos.get(j).getValue())){
                                list.get(i).setFqqdmc(tXtdmVos.get(j).getName());
                            }
                        }
                        if(tXtdmVos.get(j).getFldm().equals("YWQQZT"))
                        {
                            if(list.get(i).getClzt().equals(tXtdmVos.get(j).getValue()))
                            {
                                list.get(i).setClztmc(tXtdmVos.get(j).getName());
                            }
                        }
                    }
                }
            }
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询历史异常明细信息出错", e);
            throw new MyException("查询历史异常明细信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryYybByCzr(YcmxlsVo ycmxlsVo) {
        ResponseObject obj = new ResponseObject();
        try{
            List<YcmxlsVo> list = ycmxlsDao.queryYybByCzr(ycmxlsVo);
            obj.setData(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询营业部信息出错", e);
            throw new MyException("查询营业部信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryZhTxtdm(YcmxlsVo ycmxlsVo) {
        ResponseObject obj = new ResponseObject();
        try{
            List<YcmxlsVo> list = ycmxlsDao.queryZhTxtdm(ycmxlsVo);
            obj.setData(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询字典信息出错", e);
            throw new MyException("查询字典信息出错",e);
        }
        return obj;
    }
}
